﻿Imports System.Data.SqlClient

Public Class frmRendimientoStandard
    Private mstrCondicion As String
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmRendimientoStandard_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró la definición de rendimientos")
        Usuario.Ubicacion("Ventana Principal")
    End Sub
    Private Sub frmRendimientoStandard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        dgAutobuses.DefaultCellStyle.Font = New Font("Segoe UI", 10, FontStyle.Regular)

        Bitacora.RegistraActividad("Ingresó a la definición de rendimientos")
        Usuario.Ubicacion("Definición de Rendimientos")

        txtBuscar.Clear()
        CargaAutobuses()
        txtBuscar.Focus()

    End Sub
    Sub CargaAutobuses()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgAutobuses.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscar.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "SELECT IdAutobus,NoEconomico,Nombre,cCodigoC01,cIdClien01,Cod_Vehiculo,A.Importe02 FROM Autobuses A, Rutas R WHERE A.IdRuta=R.IdRuta AND (UPPER(cCodigoC01) LIKE '" & Filtro & "' " & mstrCondicion & " OR UPPER(NoEconomico) LIKE '" & Filtro & "' " & mstrCondicion & " OR UPPER(Nombre) LIKE '" & Filtro & "' " & mstrCondicion & ")"

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
             
                dgAutobuses.Rows.Add(rdBuscar("IdAutobus"), rdBuscar("cCodigoC01").ToString.Trim, rdBuscar("NoEconomico").ToString.Trim, rdBuscar("Nombre"), rdBuscar("Importe02"))
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub

    Private Sub txtBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
        CargaAutobuses()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub
    Private Sub FinDeEdicion(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgAutobuses.CellEndEdit
        'Añadir aqui las validaciones necesarias
        Try
            If dgAutobuses.CurrentCell.ColumnIndex = 4 Then
                'Dim _Producto As DataRow = NombresProductos.Rows.Find(Me.dgProductos(e.ColumnIndex, e.RowIndex).Value)
                'Me.dgProductos(0, e.RowIndex).Value = _Producto("Nombre")
                If dgAutobuses.CurrentRow.Cells(4).Value = "" Then
                    dgAutobuses.CurrentRow.Cells(4).Value = 0
                End If
                Call SetSqlCitra("UPDATE Autobuses SET Importe02=" & dgAutobuses.CurrentRow.Cells(4).Value & "WHERE IdAutobus=" & dgAutobuses.CurrentRow.Cells(0).Value)
            End If


        Catch ex As Exception
            '.... Accion en caso de excepcion
        End Try
    End Sub

    Private Sub dgAutobuses_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgAutobuses.CellContentClick

    End Sub
End Class